package com.cmcc.linlaker.ui.upload;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.VolleyError;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.upload.adapter.TaskAdapter;
import com.cmcc.linlaker.ui.upload.adapter.UploadQuestionAdapter;
import com.cmcc.linlaker.ui.upload.adapter.UploadQuestionAdapter2;
import com.cmcc.linlaker.ui.upload.bean.CheckingTaskBean;
import com.cmcc.linlaker.ui.upload.bean.UploadQuestionBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CheckZxActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_zx);

        BarUtils.initStatusBar(this);

        initView();
    }

    private UploadQuestionAdapter2 adapter;
    private RecyclerView recycler_checking;
    private List<UploadQuestionBean.UploadQuestionData> uploadList = new ArrayList<>();

    public void initView() {
        recycler_checking = findViewById(R.id.recycler_checking);
        setRecyclerView(recycler_checking, uploadList);

        getTaskList();
    }

    private void setRecyclerView(RecyclerView recycler_view, final List<UploadQuestionBean.UploadQuestionData> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));

        recycler_view.setNestedScrollingEnabled(false);

        adapter = new UploadQuestionAdapter2(this, itemList);
        adapter.setOnItemClickListener(new UploadQuestionAdapter2.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(CheckZxActivity.this, UploadZxActivity.class);
                intent.putExtra("problemBean", itemList.get(position));
                startActivityForResult(intent, 100);
            }
        });
        recycler_view.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 100) {
            getTaskList();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    public void back(View view) {
        finish();
    }

    /**
     * 新建巡查任务
     */
    public void newCheckingTask(View view) {
        Intent intent = new Intent(CheckZxActivity.this, NewCheckingTaskActivity.class);
        startActivity(intent);
    }

    /**
     * 获取列表
     */
    private void getTaskList() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("problemSort", "3");
//        params.put("reportUser", "3");
        params.put("reportUser", SPHelper.getUid());
        helper.sendPOSTRequest(ApiDefine.PROBLEM_ZX_LIST, UploadQuestionBean.class, params, true, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    UploadQuestionBean bean = (UploadQuestionBean) data;
                    if (bean.code.equals("0")) {
                        List<UploadQuestionBean.UploadQuestionData> list = bean.rows;
                        if (list != null && list.size() > 0) {
                            uploadList.clear();
                            uploadList.addAll(list);
                            adapter.notifyDataSetChanged();
                        } else {
                            uploadList.clear();
                            adapter.notifyDataSetChanged();
                        }

                        if (uploadList.size() > 0) {
                            recycler_checking.setVisibility(View.VISIBLE);
                            findViewById(R.id.layout_no_data).setVisibility(View.GONE);
                        } else {
                            recycler_checking.setVisibility(View.GONE);
                            findViewById(R.id.layout_no_data).setVisibility(View.VISIBLE);
                        }
                    }else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(CheckZxActivity.this);
                    }
                }
            }
        });
    }

}
